package com.zhousb.deviceManager.exception;


import com.zhousb.deviceManager.common.R;
import com.zhousb.deviceManager.common.ResponseResult;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.http.converter.HttpMessageConversionException;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {


    @ExceptionHandler(CustomerException.class)
    public ResponseResult handleCustomerException(HttpServletRequest request, CustomerException e){
        return R.fail(e.getMsg(),e);
    }


    @ExceptionHandler(MissingServletRequestParameterException.class)
    public ResponseResult handleMethodArgumentNotValid(MissingServletRequestParameterException e) {
        log.error("校验参数异常信息:{}",e.getMessage());
        return R.fail("请求参数不正确",e);
    }

    /**
     * 捕获MethodArgumentNotValidException异常
     * @param e
     * @return
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public ResponseResult handleMethodArgumentNotValid(MethodArgumentNotValidException e) {
        log.error("校验参数异常信息:{}",e.getMessage());
        BindingResult bindingResult = e.getBindingResult();
        for (FieldError fieldError : bindingResult.getFieldErrors()) {
            return R.fail(fieldError.getDefaultMessage(),fieldError);
        }
        return R.fail(e.getMessage(),e);
    }

    /**
     * 空指针异常
     */
    @ExceptionHandler(NullPointerException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResponseResult nullPointerExceptionHandler(NullPointerException e) {
        log.error("空指针异常信息:{}",e.getMessage());
        e.printStackTrace();
        return R.fail("数据空异常",e);
    }

    /**
     * 类型转换异常
     */
    @ExceptionHandler(ClassCastException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResponseResult classCastExceptionHandler(ClassCastException e) {
        log.error("类型转换异常信息:{}",e.getMessage());
        return R.fail("类型转换异常信息",e);
    }

    /**
     * 类型转换异常
     * @param e
     * @return
     */
    @ExceptionHandler(HttpMessageConversionException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResponseResult httpMessageConversionExceptionHandler(HttpMessageConversionException e) {
        log.error("请求参数出错:{}",e.getMessage());
        return R.fail("请求参数出错",e);
    }
    /**
     * 数据库相关异常
     * @param e
     * @return
     */
    @ExceptionHandler(BadSqlGrammarException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResponseResult sqlExceptionHandler(BadSqlGrammarException e){
        log.error("数据库操作出错:{}",e.getMessage());
        e.printStackTrace();
        return R.fail("数据库异常",e);
    }

    @ExceptionHandler(Exception.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResponseResult sqlExceptionHandler(Exception e){
        e.printStackTrace();
        return R.fail("请求出错,请检查",e);
    }

    @ExceptionHandler(UsernameNotFoundException.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public ResponseResult usernameNotFoundException(UsernameNotFoundException e){
        e.printStackTrace();
        return R.fail("用户名或密码出错",e);
    }

}
